package com.netease.demo.shortvideo.shortvideo.videoprocess;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

import com.netease.demo.shortvideo.shortvideo.videoprocess.model.VideoProcessOptions;
import com.netease.transcoding.TranscodingAPI;
import com.netease.transcoding.TranscodingNative;

import static com.netease.transcoding.TranscodingAPI.LSMediaTrascodingErrCode_AudioVolumeAdjustParamError;
import static com.netease.transcoding.TranscodingAPI.LSMediaTrascodingErrCode_WaterMarkTimeTooLongParamError;

/**
 * Created by hzxuwen on 2017/4/10.
 */

public class VideoProcessController {
    private static final String TAG = VideoProcessController.class.getSimpleName();

    public interface VideoProcessCallback {
        void onVideoProcessSuccess();
        void onVideoProcessFailed(int code);
        void onVideoSnapshotSuccess();
        void onVideoProcessUpdate(int process);
    }

    private Context context;
    private AsyncTask videoProcessTask;
    private AsyncTask snapshotTask;
    private VideoProcessCallback callback;

    public VideoProcessController(Context context, VideoProcessCallback callback) {
        this.context = context;
        this.callback = callback;
    }

    public void startCombination(VideoProcessOptions videoProcessOptions) {
        videoProcessTask = new AsyncTask<Object, Integer, Integer>() {

            @Override
            protected Integer doInBackground(Object[] params) {
                final TranscodingAPI.InputFilePara inputFilePara = (TranscodingAPI.InputFilePara) params[0];
                final TranscodingAPI.WaterMarkPara waterMarkPara = (TranscodingAPI.WaterMarkPara) params[1];
                final TranscodingAPI.ChartletPara chartletPara = (TranscodingAPI.ChartletPara) params[2];
                final TranscodingAPI.CropPara cropPara = (TranscodingAPI.CropPara) params[3];
                final TranscodingAPI.FileCutPara fileCutPara = (TranscodingAPI.FileCutPara) params[4];
                final TranscodingAPI.MixAudioPara mixAudioPara = (TranscodingAPI.MixAudioPara) params[5];
                final TranscodingAPI.TranscodePara transcodePara = (TranscodingAPI.TranscodePara) params[6];
                final TranscodingAPI.OutputFilePara outputFilePara = (TranscodingAPI.OutputFilePara) params[7];
                transcodePara.setCallBack(new TranscodingNative.NativeCallBack() {
                    @Override
                    public void progress(int progress, int total) {
                        publishProgress(progress);
                    }
                });
                return TranscodingAPI.getInstance().VODProcess(inputFilePara,
                        waterMarkPara, chartletPara,
                        cropPara, fileCutPara,
                        mixAudioPara, transcodePara,
                        outputFilePara);
            }

            @Override
            protected void onPreExecute() {
            }

            @Override
            protected void onCancelled() {
            }

            @Override
            protected void onProgressUpdate(Integer... values) {
                callback.onVideoProcessUpdate(values[0]);
            }

            @Override
            protected void onPostExecute(Integer ret) {
                Log.e(TAG, "短视频处理：" + ret);
                if (ret == TranscodingAPI.LSMediaTrascodingErrCode_InputFileParseError) {
                    Log.e(TAG, "短视频处理失败，解析输入文件失败");
                } else if (ret == TranscodingAPI.LSMediaTrascodingErrCode_InputFileParamError) {
                    Log.e(TAG, "短视频处理失败，输入文件参数出错");
                } else if (ret == TranscodingAPI.LSMediaTrascodingErrCode_MultiInputFileFadeTimeParamError) {
                    Log.e(TAG, "短视频处理失败，多输入文件时淡入淡出参数出错");
                } else if (ret == TranscodingAPI.LSMediaTrascodingErrCode_CutFileTimeParamError) {
                    Log.e(TAG, "短视频处理失败，裁剪文件时长参数出错");
                } else if (ret == TranscodingAPI.LSMediaTrascodingErrCode_MixAudioVolumeParamError) {
                    Log.e(TAG, "短视频处理失败，混音音量参数出错");
                } else if (ret == TranscodingAPI.LSMediaTrascodingErrCode_MixAudioFadeTimeParamError) {
                    Log.e(TAG, "短视频处理失败，混音淡入淡出参数出错");
                } else if (ret == TranscodingAPI.LSMediaTrascodingErrCode_CropSizeTooLargeParamError) {
                    Log.e(TAG, "短视频处理失败，裁剪的尺寸和位置参数出错");
                } else if (ret == TranscodingAPI.LSMediaTrascodingErrCode_WaterMarkSizeTooLargeOrSmallParamError) {
                    Log.e(TAG, "短视频处理失败，水印位置和尺寸参数出错");
                } else if (ret == LSMediaTrascodingErrCode_WaterMarkTimeTooLongParamError) {
                    Log.e(TAG, "短视频处理失败，水印显示时间不在文件时长范围内");
                } else if (ret == LSMediaTrascodingErrCode_AudioVolumeAdjustParamError) {
                    Log.e(TAG, "短视频处理失败，视频音量调节参数出错");
                } else {
                    Log.e(TAG, "短视频处理结束");
                    callback.onVideoProcessSuccess();
                    return;
                }
                callback.onVideoProcessFailed(ret);
            }
        }.execute(videoProcessOptions.getInputFilePara(),
                videoProcessOptions.getWaterMarkPara(),
                videoProcessOptions.getChartletPara(),
                videoProcessOptions.getCropPara(),
                videoProcessOptions.getFileCutPara(),
                videoProcessOptions.getMixAudioPara(),
                videoProcessOptions.getTranscodePara(),
                videoProcessOptions.getOutputFilePara());

    }

    public void startSnapShot(final TranscodingAPI.SnapshotPara para) {
        snapshotTask = new AsyncTask<TranscodingAPI.SnapshotPara,Integer,Integer>(){

            ProgressDialog dialog;
            @Override
            protected Integer doInBackground(TranscodingAPI.SnapshotPara... params) {
                final TranscodingAPI.SnapshotPara snapshotPara = params[0];
                snapshotPara.setCallBack(new TranscodingNative.NativeCallBack(){
                    @Override
                    public void progress(int progress, int total) {
                        dialog.setMax(total);
                        publishProgress(progress);
                    }
                });
                return TranscodingAPI.getInstance().snapShot(snapshotPara);
            }

            @Override
            protected void onPreExecute() {
                dialog = new ProgressDialog(context);
                dialog.setMessage("开始截图");
                dialog.setCancelable(true);
                dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                dialog.show();
            }

            @Override
            protected void onCancelled() {
                if(dialog != null && dialog.isShowing()){
                    dialog.dismiss();
                    dialog = null;
                }
            }

            @Override
            protected void onProgressUpdate(Integer[] values) {
                dialog.setMessage("截图中，请稍后...");
                dialog.setProgress(values[0]);
            }

            @Override
            protected void onPostExecute(Integer ret) {
                dialog.dismiss();
                dialog = null;
                if (ret == TranscodingAPI.LSMediaTrascodingErrCode_InputFileParseError) {
                    Log.i(TAG, "截图失败，解析输入文件失败");
                } else if (ret != TranscodingAPI.LSMediaTrascodingErrCode_NO) {
                    Log.i(TAG, "截图失败，请仔细检查参数");
                } else {
                    Log.i(TAG, "截图已结束");
                    callback.onVideoSnapshotSuccess();
                }
            }
        }.execute(para);
    }
}
